import java.util.List;

public class DeleteTask {
	
	Task taskToBeDeleted;
	
	public DeleteTask() {
		taskToBeDeleted = new Task();
	}

	
	public void execute(UserInputElements userInputElements,
			List<Task> tasksInTempStorage, List<Task> tasksOnDisplay) {
		String message;
		int numberOfTasksDisplayed = tasksOnDisplay.size();
		
		//TODO: move down later nicely
		/*int indexOfDeleteLine = Integer.parseInt(userInputElements
				.getTaskDescription());
		boolean outOfBounds = ((indexOfDeleteLine <= 0) || (indexOfDeleteLine > numberOfTasksDisplayed));
		*/
		
		if(hasIncorrectDeleteArguments(userInputElements))
		{
			message = "incorrect arguments";
		}
		
		else if (Integer.parseInt(userInputElements.getTaskDescription()) <= 0 || Integer.parseInt(userInputElements
				.getTaskDescription()) > numberOfTasksDisplayed) {
			message = "out of bounds";
		}
		
		else {
			this.deleteFromTempStorage(userInputElements, tasksInTempStorage, tasksOnDisplay);
			message = "deleted from task list successfully";
		}
		TaskO.taskOMainScreen.appendOutput(message);
		System.out.println(message);
	}
	
	private boolean hasIncorrectDeleteArguments(
			UserInputElements userInputElements) {
		char[] parameters;
		parameters = userInputElements.getTaskDescription().toCharArray();
		boolean isIncorrectArgs = false;

		for (int i = 0; i < parameters.length; i++) {
			if (!Character.isDigit(parameters[i])) {
				isIncorrectArgs = true;
			}
		}
		return isIncorrectArgs;
	}

	public void deleteFromTempStorage(UserInputElements userInputElements,
			List<Task> tasksInTempStorage, List<Task> tasksOnDisplay) {
		int taskNumberToBeRemoved = Integer.parseInt(userInputElements.getTaskDescription());
		taskToBeDeleted = tasksOnDisplay.get(taskNumberToBeRemoved-1);
		int taskIdOfAllTasks = taskToBeDeleted.getTaskId();
		
		tasksOnDisplay.remove(taskNumberToBeRemoved-1);
		tasksInTempStorage.remove(taskIdOfAllTasks-1);
		
		
		for(int i=0; i<tasksOnDisplay.size(); i++) {
			int taskId = tasksOnDisplay.get(i).getTaskId();
			if(taskId > taskIdOfAllTasks) {
			tasksOnDisplay.get(i).setTaskId(taskId-1);
			}
		}
		for(int i=0; i<tasksInTempStorage.size(); i++) {
			int taskId1 = tasksInTempStorage.get(i).getTaskId();
			if(taskId1 > taskIdOfAllTasks) {
			tasksInTempStorage.get(i).setTaskId(taskId1-1);
			}
		}
		
	}
}
